$(function(){
    $.fn.extend({
	"AdamScroll" : function(options,inIframe){
	    var $this = $(this);
	    configs = $.extend({
		'width'      : '870',
		'overflowX'  : 'hidden',
		'overflowY'  : '',
		'height'     : '',
		'step'       : '100',
		'btnLWidth'  : '30',
		'btnHeight'  : '90',
		'btnLImg'    : 'leftBtn.gif',
		'btnRImg'    : 'rightBtn.gif'
	    }, options);

	    $this.css({
		'width'     : configs["width"]+"px",
		'height'    : configs["height"],
		'overflowX' : configs["overflowX"],
		'overflowY' : configs["overflowY"]
	    });

	    var btnPosition = calculateBtnPos();

	    var timeA, timeB;

	    var $leftBtn = $("<div>").mousedown(function() {
		    timeA = setInterval(turnLeft, configs['step']);
	    }).mouseup(function(){
		    clearInterval(timeA);
	    }).click(function(){
		    turnLeft();
	    }).css({
            'width'      : configs['btnLWidth']+'px',
            'height'     : configs['btnHeight']+'px',
            'position'   : 'absolute',
            'top'        : '200px',
            'background' : 'url('+configs['btnLImg']+')',
            'left'       : btnPosition+'px',
            'cursor'     : 'pointer'
	    }).appendTo($("body")).attr('id', 'leftBtn');

	    var $rightBtn = $("<div>").mousedown(function() {
		    timeB = setInterval(turnRight, configs['step']);
	    }).mouseup(function(){
		    clearInterval(timeB);
	    }).click(function(){
		    turnRight();
	    }).css({
            'width'      : configs['btnLWidth']+'px',
            'height'     : configs['btnHeight']+'px',
            'position'   : 'absolute',
            'top'        : '200px',
            'background' : 'url('+configs['btnRImg']+')',
            'right'      : btnPosition+'px',
            'cursor'     : 'pointer'
	    }).appendTo($("body")).attr('id', 'rightBtn');

        var win = window;
        if ( inIframe ) {
            win = window.top;
        }

	    $this.data("lastScroll", 0);
	    $this.data("endOfLeft", true);
	    $this.data("endOfRight", false);

	    function turnLeft(){
		$this[0].scrollLeft = $this[0].scrollLeft-500;
		if ( $this[0].scrollLeft == $this.data["lastScroll"] ) {
		    $this.data("endOfLeft", true);
		} else {
		    $this.data("endOfLeft", false);
		}
		$this.data("lastScroll", $this[0].scrollLeft);
	    }

	    function turnRight() {
		$this[0].scrollLeft = $this[0].scrollLeft+500;
		$this.data("lastScroll", $this[0].scrollLeft);
	    }

	    function calculateBtnPos() {
		var bodyWidth = document.body.offsetWidth;
		var tabWidth= $('#boxTable').width();
	        return ((bodyWidth-tabWidth)/2 - configs["btnLWidth"])*2/3;
	    }
	}
    });
});
